package edu.ncist.wang.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.compress.utils.IOUtils;

public class TarUti{

	
	public static void deGzipArchive(String dir, String filepath)throws Exception {
		//不管是否存在先删除，再创建避免了文件的重复节省了空间
		DeleteFile.DeleteFolder(dir);
		File f=new File(dir);
		
		boolean mkdir = f.mkdir();
			
		
final File input = new File(filepath);
final InputStream is = new FileInputStream(input);
	final CompressorInputStream in = new GzipCompressorInputStream(is, true);
	TarArchiveInputStream tin = new TarArchiveInputStream(in);
	TarArchiveEntry entry = tin.getNextTarEntry();
	while (entry != null) {
File archiveEntry = new File(dir, entry.getName());
archiveEntry.getParentFile().mkdirs();
if (entry.isDirectory()) {
archiveEntry.mkdir();
entry = tin.getNextTarEntry();
continue;
}
OutputStream out = new FileOutputStream(archiveEntry);
IOUtils.copy(tin, out);
out.close();
entry = tin.getNextTarEntry();
}
in.close();
tin.close();
}
	
	
}


